//
//  DDropView.swift
//  Drop
//
//  Created by hushaohua on 2017/12/20.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit

class DDropView: UIView, UIDynamicAnimatorDelegate {
    
    override init(frame: CGRect) {
        self.dripSize = frame.size;
        super.init(frame: frame);
        self.addSubview(self.dripView);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func dynamicAnimatorDidPause(_ animator: UIDynamicAnimator) {
        print("dynamicAnimatorDidPause")
    }
    func dynamicAnimatorWillResume(_ animator: UIDynamicAnimator) {
        print("dynamicAnimatorWillResume")
    }
    
    lazy var animator:UIDynamicAnimator = {
        var animator = UIDynamicAnimator(referenceView: self);
        animator.delegate = self;
        return animator;
    }();
    
    lazy var gravityBehavior:UIGravityBehavior = {
        var behavior:UIGravityBehavior = UIGravityBehavior(items: [self.dripView]);
        return behavior;
    }();
    
    func shapePath() -> UIBezierPath {
        let width:CGFloat = self.dripSize.width;
        let height:CGFloat = self.dripSize.height;
        let path:UIBezierPath = UIBezierPath();
        path.lineWidth = 1;
        path.move(to: CGPoint(x:(width / 2),y:0));
        path.addCurve(to: CGPoint(x:(width / 2),y:0), controlPoint1: CGPoint(x:0,y:height), controlPoint2: CGPoint(x:width,y:height));
        path.close();
        return path;
    }
    
    lazy var dripShapeLayer:CAShapeLayer = {
        let shape:CAShapeLayer = CAShapeLayer();
        shape.fillColor = UIColor.black.cgColor;
        shape.strokeColor = UIColor.black.cgColor;
        shape.lineWidth = 1;
        return shape;
    }();
    
    func setDripColor(_ color:UIColor) -> Void {
        self.dripShapeLayer.fillColor = color.cgColor;
        self.dripShapeLayer.strokeColor = color.cgColor;
    }
    
    lazy var dripView:UIView = {
        let width:CGFloat = self.dripSize.width;
        let height:CGFloat = self.dripSize.height;
        var view:UIView = UIView(frame: CGRect(x:(self.bounds.width - width) / 2,y:0,width:width,height:height));
        view.layer.addSublayer(self.dripShapeLayer);
        return view;
    }();
    
    var dripSize:CGSize;
    func layoutDripView() -> Void{
        let width:CGFloat = dripSize.width;
        let height:CGFloat = dripSize.height;
        self.dripView.frame = CGRect(x:(self.bounds.width - width) / 2,y:0,width:width,height:height);
        self.dripShapeLayer.path = self.shapePath().cgPath;
        self.dripShapeLayer.frame = self.dripView.bounds;
    }
    
    override var frame: CGRect{
        didSet{
            self.layoutDripView();
        }
    }
    
    func play() -> Void {
        self.animator.removeBehavior(self.gravityBehavior);
        self.dripView.frame.origin.y = 0;
        self.layoutDripView();
        self.animator.addBehavior(self.gravityBehavior);
    }
    
    func stop() -> Void {
        self.animator.removeBehavior(self.gravityBehavior);
    }
    
}
